<template>
  <aside class="btn-box">
    <el-row>
      <el-col :span="2">
        <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="addFun">新增</el-button>
        <add-new-dialog ref="addNew" @reloadFun="reloadFun" />
      </el-col>
      <el-col :span="2">
        <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="searchBtn">查找</el-button>
        <search-dialog ref="search" @search="searchFun" />
      </el-col>
      <el-col v-if="tbStatus === '0'" :span="3">
        <el-button type="primary" size="mini" icon="el-icon-coordinate" @click="selectedCheck">批量审核</el-button>
      </el-col>
      <el-col :span="6">
        <el-radio-group v-model="radio1" size="small" @change="tbStatusChange">
          <el-radio :label="'0'">待审核</el-radio>
          <el-radio :label="'1'">正常</el-radio>
          <el-radio :label="'2'">已拒绝</el-radio>
        </el-radio-group>
      </el-col>
    </el-row>
  </aside>
</template>

<script>
import { checkTaobaoImpowerApi, resetTaobaoImpowerPicApi } from '@/api/agent'
import { getNewTime } from '@/utils/index'
import { drawPhoto } from '@/utils/drawPhoto'
import { conversion } from '@/utils/rules'
import addNewDialog from './tb-add-new'
import searchDialog from './tb-search'
export default {
  components: {
    addNewDialog,
    searchDialog
  },
  props: {
    tbStatus: {
      type: String,
      default: '0'
    },
    selectArr: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      radio1: this.tbStatus
    }
  },
  methods: {
    searchBtn() {
      this.$refs['search'].dialogShow = true
    },
    searchFun(val) {
      this.$emit('search', val)
    },
    reloadFun() {
      this.$emit('reloadFun')
    },
    addFun() {
      this.$refs['addNew'].dialogShow = true
    },
    tbStatusChange(val) {
      console.log(val)
      this.$emit('statusChange', val)
    },
    selectedCheck() {
      if (this.selectArr.length === 0) {
        this.$message.info('请先选择要审核的数据')
        return false
      }
      console.log(this.selectArr)
      this.$prompt('请输入授权时长（天数），默认天数为365天', '淘宝授权确认审核', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定',
        cancelButtonText: '拒绝',
        inputPlaceholder: '请输入授权天数，若不输入默认365天'
      }).then(({ value }) => {
        const tb_date = value ? getNewTime(Number(value)) : getNewTime(365)
        console.log(tb_date)
        this.selectArr.forEach(item => {
          console.log(item)
          const data = {
            anent_id: item.anent_id,
            brand_id: item.brand_id,
            tb_status: '1',
            tb_date: tb_date
          }
          console.log(data)
          checkTaobaoImpowerApi(data).then(res => {
            console.log(res)
            if (res.code === 200) {
              const textArr = [
                {
                  text: item.anent_content.anent_name,
                  x: 360,
                  y: 360
                },
                {
                  text: conversion(item.anent_content.people_card_num, 6, 14, 8),
                  x: 360,
                  y: 398
                },
                {
                  text: item.tb_name,
                  x: 360,
                  y: 430
                },
                {
                  text: item.code_num,
                  x: 329,
                  y: 500
                },
                {
                  text: item.brand_content.brand_name,
                  x: 480,
                  y: 500
                },
                {
                  text: '淘宝平台',
                  x: 384,
                  y: 535
                },
                {
                  text: item.tb_address,
                  x: 260,
                  y: 570
                },
                {
                  text: tb_date,
                  x: 290,
                  y: 675
                }
              ]
              let warrantImg = ''
              drawPhoto(res.img.tb_approval_img, { width: '787', height: '1113' }, textArr, imgUrl => {
                console.log(imgUrl)
                warrantImg = imgUrl
                const that = this
                const imgData = {
                  anent_id: item.anent_id,
                  brand_id: item.brand_id,
                  code_num: item.code_num,
                  tb_approval_img: warrantImg
                }
                console.log(imgData)
                resetTaobaoImpowerPicApi(imgData).then(res => {
                  console.log(res)
                  if (res.code === 200) {
                    that.$message.success('淘宝授权成功')
                    this.$emit('reloadFun')
                  }
                })
              })
            } else if (res.code === 400) {
              this.$message.warning('错误!请重新审核')
            }
          })
        })
      }).catch((action) => {
        if (action === 'cancel') {
          const that = this
          this.selectArr.forEach(item => {
            const data = {
              anent_id: item.anent_id,
              brand_id: item.brand_id,
              tb_status: '2'
            }
            checkTaobaoImpowerApi(data).then(res => {
              if (res.code === 200) {
                that.$message.warning('授权申请已拒绝!')
                this.$emit('reloadFun')
              }
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
